const express = require('express')
const app = express()
const port = 3000

const indexRouter = require('./router/index')
const homeRouter = require('./router/home')

// 开启一个静态资源服务
// app.use(express.static('dist'))

// const path = require('path')
// app.use('/', express.static(path.join(__dirname, 'dist')))

// app.get('/home', (req, res) => {
//   res.send('home')
// })

// app.get('/home/:id', (req, res) => {
//   res.send('home/2')
// })

// app.post('/user',(req,res)=>{
//   console.log('post')
// })

// 解决跨域
app.use(require("cors")())

// post 路由获取参数 要解析 老版本的要下载 body-parse
//响应form表单格式 name=rain&age=18
app.use(express.urlencoded({extended:false}))
//响应json格式 {name:"rain",age:18}
app.use(express.json())

//应用级别中间件
app.use((req,res,next)=>{
  //验证token
  next()
})
//应用级别中间件
app.use('/index',indexRouter)
app.use('/home',homeRouter)

app.use((req, res, next) => {
  res.status(404).send("Sorry can't find that!")
})

app.listen(port, () => {
  console.log(`Example app listening on port ${port}`)
})